package com.jet.goods.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.jet.core.utils.IdUtil;
import com.jet.goods.channel.factory.BarcodeChannel;
import com.jet.goods.channel.factory.BarcodeChannelFactory;
import com.jet.goods.domain.entity.Goods;
import com.jet.goods.mapper.GoodsMapper;
import com.jet.goods.service.IGoodsService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author author
 * @since 2025-03-04
 */
@Service
public class GoodsServiceImpl extends ServiceImpl<GoodsMapper, Goods> implements IGoodsService {

    private static final String channelCode = "wanwei";

    @Override
    public Goods findByBarcode(String barcode) {
        Wrapper<Goods> wrapper = new LambdaQueryWrapper<Goods>()
                .eq(Goods::getBarcode, barcode);
        Goods goods = super.getOne(wrapper);
        if (goods != null) {
            return goods;
        }
        // 调用外部接口
        BarcodeChannel channel = BarcodeChannelFactory.get(channelCode);
        goods = channel.getBarcode(barcode);
        if (goods == null) {
            return null;
        }
        goods.setId(IdUtil.generateLower());
        super.save(goods);
        return goods;
    }
}
